//根据公司ID获取经营部门
$.fn.jybmCombobox = function (paramOptions) {
    var options = {
        url:$.webPath() + '/organ/gsjg/jybm?gsjgId=' + paramOptions.gsjgId,
        valueField:'id',
        textField:'qc',
        multiple:false,
        panelHeight:'200'
    }
    this.combobox(options);
}

//自购物资计划申报公司
$.fn.zgwzjhSbgsCombobox = function (paramOptions) {
    var options = {
        url:$.webPath() + '/jhgl/wzjhgl/zgwzjhSbgs',
        valueField:'id',
        textField:'name',
        multiple:false,
        panelHeight:'200'
    };
    $.extend(options, paramOptions);
    this.combobox(options);
}

$.fn.ljgsjgtree = function (type, gsjgId) {
    var option = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=' + type + '&gsjgId=' + gsjgId,
        panelHeight:'200',
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(option, arguments));
}

//公司机构树
$.fn.gsjgtree = function (type) {
    var option = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=' + type,
        panelHeight:'200',
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(option, arguments));
}

//公司经营下拉树机构树
$.fn.jygsjgcombotree = function () {
    var option = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree_jy',
        panelHeight:'250',
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(option, arguments));
}
//无权限公司机构树
$.fn.jygsjgwqxcombotree = function () {
    var option = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree_wqx',
        panelHeight:'250',
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(option, arguments));
}
//所属机构
$.fn.ssgsjgcombotree = function () {
    var option = {
        url:$.webPath() + '/organ/gsjg/gsjg_sstree',
        panelHeight:'250',
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(option, arguments));
}

//保供业务部门combobox
$.fn.bgywbmcombobox = function () {
    var option = {
        url:$.webPath() + '/organ/gsjg/bgywbm',
        panelHeight:'250',
        valueField:'id',
        textField:'qc'
    }

    this.combobox($.createArg(option, arguments));
}

//公司经营下拉树机构树
$.fn.jygsjgcombotreechild = function (rootId) {
    var option = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree_jychild?rootId=' + rootId,
        panelHeight:'250',
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(option, arguments));
}

$.fn.ljtreegrid = function (type, gsjgId) {
    var option = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=' + type + '&gsjgId=' + gsjgId,
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.tree($.createArg(option, arguments));
}

//公司机构树
$.fn.gsjgtreegrid = function (type) {
    var option = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=' + type,
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.tree($.createArg(option, arguments));
}

//公司经营机构树
$.fn.jygsjgtree = function () {
    var option = {
        url:$.webPath() + "/organ/gsjg/gsjg_tree_jy",
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.tree($.createArg(option, arguments));
}

$.fn.gjbmcombobox = function () {
    var option = {
        url:$.webPath() + "/organ/gsjg/childgsjg_json?type=0",
        valueField:'id',
        textField:'jc',
        multiple:false,
        panelHeight:'100'
    }

    this.combobox($.createArg(option, arguments));
}

$.fn.jgrytree = function (ryobj, ryvalue) {
    var gsoption = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=1',
        panelHeight:'200',
        onChange:function (newValue, oldValue) {
            if (newValue != oldValue) {
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygListByGs?zgjgId=" + newValue);
                    ryobj.combobox("clear");
                }
            }
        },
        onSelect:function (record) {
            if (ryobj) {
                ryobj.combobox("reload", $.webPath() + "/sys/yg/ygListByGs?zgjgId=" + record.id);
                if (ryvalue)
                    ryobj.combobox("setValue", ryvalue);
                else
                    ryobj.combobox("clear");
            }
        },
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(gsoption, arguments));
}

//公司机构树级联部门
$.fn.jgbmtree = function (bmobj, ryobj, bmvalue, ryvalue) {
    var gsoption = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=1',
        panelHeight:'200',
        lines:true,
        onChange:function (newValue, oldValue) {
            if (newValue != oldValue) {
                bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childgsjg_json?type=0&gsjgId=" + newValue);
                bmobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + newValue);
                    ryobj.combobox("clear");
                }
            }
        },
        onSelect:function (record) {
            bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childgsjg_json?type=0&gsjgId=" + record.id);
            if (bmvalue) {
                bmobj.combobox("select", bmvalue);
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + bmvalue);
                }
            } else {
                bmobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + record.id);
                }
            }
            if (ryobj) {
                if (ryvalue) {
                    ryobj.combobox("setValue", ryvalue);
                    ryvalue = undefined;
                } else
                    ryobj.combobox("clear");
            }
        },
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(gsoption, arguments));

    var bmoption = {
        valueField:'id',
        textField:'jc',
        multiple:false,
        panelHeight:'100',
        onChange:function (newValue, oldValue) {
            if (ryobj)
                if (newValue != oldValue) {
                    if (!newValue) {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + $(this).combotree('getValue'));
                        ryobj.combobox("clear");
                    } else {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + newValue);
                        ryobj.combobox("clear");
                    }
                }
        },
        onSelect:function (record) {
            if (ryobj) {
                ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + record.id);
                if (ryvalue) {
                    ryobj.combobox("setValue", ryvalue);
                    ryvalue = undefined;
                } else
                    ryobj.combobox("clear");
            }
        }
    }

    bmobj.combobox($.createArg(bmoption, arguments));
}

//票据专用公司机构树级联部门
$.fn.pjjgbmtree = function (bmobj, ryobj, bmvalue, ryvalue) {
    var gsoption = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=1',
        panelHeight:'200',
        lines:true,
        onChange:function (newValue, oldValue) {
            if (newValue != oldValue) {
                bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childgsjg_json?type=0&gsjgId=" + newValue);
                bmobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygAllXmList?zgjgId=" + newValue);
                    ryobj.combobox("clear");
                }
            }
        },
        onSelect:function (record) {
            bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childgsjg_json?type=0&gsjgId=" + record.id);
            if (bmvalue) {
                bmobj.combobox("select", bmvalue);
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygAllXmList?zgjgId=" + bmvalue);
                }
            } else {
                bmobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygAllXmList?zgjgId=" + record.id);
                }
            }
            if (ryobj) {
                if (ryvalue) {
                    ryobj.combobox("setValue", ryvalue);
                    ryvalue = undefined;
                } else
                    ryobj.combobox("clear");
            }
        },
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(gsoption, arguments));

    var bmoption = {
        valueField:'id',
        textField:'jc',
        multiple:false,
        panelHeight:'100',
        onChange:function (newValue, oldValue) {
            if (ryobj)
                if (newValue != oldValue) {
                    if (!newValue) {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygAllXmList?zgjgId=" + $(this).combotree('getValue'));
                        ryobj.combobox("clear");
                    } else {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygAllXmList?zgjgId=" + newValue);
                        ryobj.combobox("clear");
                    }
                }
        },
        onSelect:function (record) {
            if (ryobj) {
                ryobj.combobox("reload", $.webPath() + "/sys/yg/ygAllXmList?zgjgId=" + record.id);
                if (ryvalue) {
                    ryobj.combobox("setValue", ryvalue);
                    ryvalue = undefined;
                } else
                    ryobj.combobox("clear");
            }
        }
    }

    bmobj.combobox($.createArg(bmoption, arguments));
}

$.fn.jgbmtrees = function (bmobj, ryobj, bmvalue, ryvalue) {
    var gsoption = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=1',
        panelHeight:'200',
        onChange:function (newValue, oldValue) {
            if (newValue != oldValue) {
                bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childjygsjg_json?type=0&gsjgId=" + newValue);
                bmobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + newValue);
                    ryobj.combobox("clear");
                }
            }
        },
        onSelect:function (record) {
            bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childjygsjg_json?type=0&gsjgId=" + record.id);
            if (bmvalue) {
                bmobj.combobox("select", bmvalue);
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + bmvalue);
                }
            } else {
                bmobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + record.id);
                }
            }
            if (ryobj) {
                if (ryvalue)
                    ryobj.combobox("setValue", ryvalue);
                else
                    ryobj.combobox("clear");
            }
        },
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(gsoption, arguments));

    var bmoption = {
        valueField:'id',
        textField:'jc',
        multiple:false,
        panelHeight:'100',
        onChange:function (newValue, oldValue) {
            if (ryobj)
                if (newValue != oldValue) {
                    if (!newValue) {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + $(this).combotree('getValue'));
                        ryobj.combobox("clear");
                    } else {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + newValue);
                        ryobj.combobox("clear");
                    }
                }
        },
        onSelect:function (record) {
            if (ryobj) {
                ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + record.id);
                if (ryvalue)
                    ryobj.combobox("setValue", ryvalue);
                else
                    ryobj.combobox("clear");
            }
        }
    }

    bmobj.combobox($.createArg(bmoption, arguments));
}

//收款公司机构树级联部门
$.fn.skjgbmtree = function (bmobj, ryobj, khobj, bmvalue, ryvalue) {
    var gsoption = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=1',
        panelHeight:'200',
        lines:true,
        onChange:function (newValue, oldValue) {
            if (newValue != oldValue) {
                bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childgsjg_json?type=0&gsjgId=" + newValue);
                bmobj.combobox("clear");
                khobj.combobox("reload", $.webPath() + "/organ/qyjg/findAllKhByNbgsId?nbgsId=" + newValue);
                khobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + newValue);
                    ryobj.combobox("clear");
                }
            }
        },
        onSelect:function (record) {
            bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childgsjg_json?type=0&gsjgId=" + record.id);
            if (bmvalue) {
                bmobj.combobox("select", bmvalue);
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + bmvalue);
                }
            } else {
                bmobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + record.id);
                }
            }
            if (ryobj) {
                if (ryvalue) {
                    ryobj.combobox("setValue", ryvalue);
                    ryvalue = undefined;
                } else
                    ryobj.combobox("clear");
            }

            if (khobj) {
                khobj.combobox("reload", $.webPath() + "/organ/qyjg/findAllKhByNbgsId?nbgsId=" + record.id);
                khobj.combobox("clear");
            }
        },
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(gsoption, arguments));

    var bmoption = {
        valueField:'id',
        textField:'jc',
        multiple:false,
        panelHeight:'100',
        onChange:function (newValue, oldValue) {
            if (ryobj)
                if (newValue != oldValue) {
                    if (!newValue) {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + $(this).combotree('getValue'));
                        ryobj.combobox("clear");
                    } else {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + newValue);
                        ryobj.combobox("clear");
                    }
                }
        },
        onSelect:function (record) {
            if (ryobj) {
                ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + record.id);
                if (ryvalue) {
                    ryobj.combobox("setValue", ryvalue);
                    ryvalue = undefined;
                } else
                    ryobj.combobox("clear");
            }
        }
    }

    bmobj.combobox($.createArg(bmoption, arguments));
}

//收款公司机构树级联部门
$.fn.jkjgbmtree = function (bmobj, ryobj, gysobj, bmvalue, ryvalue) {
    var gsoption = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=1',
        panelHeight:'200',
        lines:true,
        onChange:function (newValue, oldValue) {
            if (newValue != oldValue) {
                bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childgsjg_json?type=0&gsjgId=" + newValue);
                bmobj.combobox("clear");
                gysobj.combobox("reload", $.webPath() + "/organ/qyjg/findAllGysByNbgsId?nbgsId=" + newValue);
                gysobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + newValue);
                    ryobj.combobox("clear");
                }
            }
        },
        onSelect:function (record) {
            bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childgsjg_json?type=0&gsjgId=" + record.id);
            if (bmvalue) {
                bmobj.combobox("select", bmvalue);
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + bmvalue);
                }
            } else {
                bmobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + record.id);
                }
            }
            if (ryobj) {
                if (ryvalue) {
                    ryobj.combobox("setValue", ryvalue);
                    ryvalue = undefined;
                } else
                    ryobj.combobox("clear");
            }

            if (gysobj) {
                gysobj.combobox("reload", $.webPath() + "/organ/qyjg/findAllGysByNbgsId?nbgsId=" + record.id);
                gysobj.combobox("clear");
            }
        },
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(gsoption, arguments));

    var bmoption = {
        valueField:'id',
        textField:'jc',
        multiple:false,
        panelHeight:'100',
        onChange:function (newValue, oldValue) {
            if (ryobj)
                if (newValue != oldValue) {
                    if (!newValue) {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + $(this).combotree('getValue'));
                        ryobj.combobox("clear");
                    } else {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + newValue);
                        ryobj.combobox("clear");
                    }
                }
        },
        onSelect:function (record) {
            if (ryobj) {
                ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + record.id);
                if (ryvalue) {
                    ryobj.combobox("setValue", ryvalue);
                    ryvalue = undefined;
                } else
                    ryobj.combobox("clear");
            }
        }
    }

    bmobj.combobox($.createArg(bmoption, arguments));
}


//保供业务部门combobox
$.fn.gyzcombobox = function () {
    var option = {
        url:$.webPath() + '/organ/gsjg/gyz_combobox?gsjgId=1',
        valueField:'id',
        textField:'jc'
    }

    this.combobox($.createArg(option, arguments));
}


//不是标准的tree数据格式的转换器
var convert = function (rows) {
    function exists(rows, parentId) {
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].id == parentId) return true;
        }
        return false;
    }

    var nodes = [];
    // 得到顶层节点
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        if (!exists(rows, row.parentId)) {
            nodes.push({
                id:row.id,
                text:row.text,
                attributes:{parentId:row.parentId},
                iconCls:row.iconCls
            });
        }
    }
    var toDo = [];
    for (var i = 0; i < nodes.length; i++) {
        toDo.push(nodes[i]);
    }
    while (toDo.length) {
        var node = toDo.shift();    // 父节点
        // 得到子节点
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (row.parentId == node.id) {
                var child = {id:row.id, state:row.state, text:row.text, iconCls:row.iconCls, attributes:{parentId:row.parentId}};
                if (node.children) {
                    node.children.push(child);
                } else {
                    node.children = [child];
                }
                toDo.push(child);
            }
        }
    }
    return nodes;
}


$.fn.jgbmtreewithnotqx = function (bmobj, ryobj, bmvalue, ryvalue) {
    var gsoption = {
        url:$.webPath() + '/organ/gsjg/gsjg_tree?type=1',
        panelHeight:'200',
        lines:true,
        onChange:function (newValue, oldValue) {
            if (newValue != oldValue) {
                bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childbmwithoutqx_json?type=0&gsjgId=" + newValue);
                bmobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + newValue);
                    ryobj.combobox("clear");
                }
            }
        },
        onSelect:function (record) {
            bmobj.combobox("reload", $.webPath() + "/organ/gsjg/childbmwithoutqx_json?type=0&gsjgId=" + record.id);
            if (bmvalue) {
                bmobj.combobox("select", bmvalue);
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + bmvalue);
                }
            } else {
                bmobj.combobox("clear");
                if (ryobj) {
                    ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + record.id);
                }
            }
            if (ryobj) {
                if (ryvalue) {
                    ryobj.combobox("setValue", ryvalue);
                    ryvalue = undefined;
                } else
                    ryobj.combobox("clear");
            }
        },
        loadFilter:function (rows) {
            return convert(rows);
        }
    }

    this.combotree($.createArg(gsoption, arguments));

    var bmoption = {
        valueField:'id',
        textField:'jc',
        multiple:false,
        panelHeight:'100',
        onChange:function (newValue, oldValue) {
            if (ryobj)
                if (newValue != oldValue) {
                    if (!newValue) {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + $(this).combotree('getValue'));
                        ryobj.combobox("clear");
                    } else {
                        ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + newValue);
                        ryobj.combobox("clear");
                    }
                }
        },
        onSelect:function (record) {
            if (ryobj) {
                ryobj.combobox("reload", $.webPath() + "/sys/yg/ygXmList?zgjgId=" + record.id);
                if (ryvalue) {
                    ryobj.combobox("setValue", ryvalue);
                    ryvalue = undefined;
                } else
                    ryobj.combobox("clear");
            }
        }
    }

    bmobj.combobox($.createArg(bmoption, arguments));
}



